import time

from code_builder.builder import create_sql, create_jsp, create_java
from code_builder.tools import read_xls, utils
from code_builder.conf import read_conf


class create_all(object):
    def create_all(self):
        data = self.get_data()
        base_path = self.get_base_path()
        # 创建sql语句
        if read_conf.getConfig("file", "create_sql") == "True":
            create_sql.create_sql_file(data, base_path)
        for table_name in data:
            dir_name = utils.to_cap_camel_case(table_name)
            target_path = "{base_path}\\{dir_name}".format(base_path=base_path, dir_name=dir_name)
            utils.create_dir(target_path)
            # 创建jsp文件
            if read_conf.getConfig("file", "create_jsp") == "True":
                create_jsp.create_jsp_file(data[table_name], target_path, table_name)
            # 创建java文件
            if read_conf.getConfig("file", "create_java") == "True":
                create_java.create_java_file(data[table_name], target_path, table_name)

    def get_data(self):
        return read_xls.read_xls().get_data()

    def get_base_path(self):
        now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
        base_path = read_conf.getConfig("path", "base_path").format(now_time=now_time)
        return base_path


if __name__ == "__main__":
    create_all().create_all()
    print("代码生成成功")